<?php


namespace App\Http\Controllers\auth;
use App\Engine\BaseController;
use App\Engine\TokenService;
use App\Engine\WxCode\demoWxCode;
use App\Exceptions\ApiException;
use App\Http\service\AuthService;
use App\Http\service\DyService;
use App\Http\service\ValidateService;
use App\Http\service\XcxService;
use App\Models\EggLog;
use App\Models\TurntableLog;
use App\Models\TxLog;
use App\Models\User;
use App\Models\Appointment as AppointmentModel;
use App\Models\UserBindGoods;
use Illuminate\Support\Facades\Log;
use function app\Utils\go;

class Auth extends BaseController
{
    public function cms_login()
    {
        $this->validate($this->request,ValidateService::$CmsLoginRule,ValidateService::$CmsLoginMessage);
        return go($this->getAuthService()->cmsLogin($this->post));
    }
    public function update_password()
    {
        $this->validate($this->request,ValidateService::$setPasswordRule,ValidateService::$setPasswordMessage);
        return go((new AuthService())->setPassword($this->post));
    }
    public function check_token()
    {
        $res=TokenService::verifyToken();
        return go(["data"=>$res]);
    }
    public function xcx_login()
    {
        $invite_uid="";
        if (isset($this->get["invite_uid"])){
            $invite_uid=$this->get["invite_uid"];
        }
        $res=(new XcxService())->xcx_login($this->get["code"],$invite_uid);
        return go($res);
    }

    /**
     * 绑定手机号码
     */
    public function bind_model()
    {
        $session_key = (new XcxService())->get_session($this->post["code"]);
        $detail = (new demoWxCode())->decryptData($this->post["encryptedData"], $this->post["iv"], $session_key);
        if (isset($detail['phoneNumber'])) {
            User::where("id", TokenService::getCurrentUid())->update(["phone" => $detail["phoneNumber"]]);
            if (isset($this->post["type"])) {
                if ($this->post["type"] == "application") {
                    $model = new AppointmentModel();
                    $post = [
                        "uid" => TokenService::getCurrentUid(),
                        "time" => date("Y-m-d H:i:s"),
                        "name" => User::where("id", TokenService::getCurrentUid())->value("nickname"),
                        "phone" => $detail["phoneNumber"],
                        "state" => 0
                    ];
                    $res = $model->create($post);
                    return go($res);
                }
            } else {
 
                Log::error($detail);
                throw new ApiException("绑定失败");
            }
            $user = User::where("id", TokenService::getCurrentUid())->first();
            return go($user);

        }
    }

    /**
     * 验证码操作码
     */
    public function check_opcode()
    {
        $code=$this->get["code"];
        $id=$this->get["id"];
        (new AuthService())->check_opcode($code);
        $log=EggLog::where("id",$id)->first();
        return go($log);
    }

    public function update_egg()
    {
        $code=$this->get["code"];
        $id=$this->get["id"];
        (new AuthService())->check_opcode($code);
        $res=EggLog::find($id)->update(["state"=>1]);
        return go($res);
    }

    public function update_bind_goods()
    {
        $code=$this->get["code"];
        $id=$this->get["id"];
        (new AuthService())->check_opcode($code);
        $res=UserBindGoods::find($id)->update(["state"=>1]);
        return go($res);
    }

    public function check_reward_opcode()
    {
        $code=$this->get["code"];
        $id=$this->get["id"];
        (new AuthService())->check_opcode($code);
        $log=TurntableLog::where("id",$id)->first();
        return go($log);
    }


    public function check_bind_opcode()
    {
        $code=$this->get["code"];
        $id=$this->get["id"];
        (new AuthService())->check_opcode($code);
        $res=UserBindGoods::where("id",$id)->first();
        return go($res);
    }


    public function check_tx_opcode()
    {
        $code=$this->get["code"];
        $id=$this->get["id"];
        (new AuthService())->check_opcode($code);
        $res=TxLog::where("id",$id)->first();
        return go($res);
    }

    public function update_reward_egg()
    {
        $code=$this->get["code"];
        $id=$this->get["id"];
        (new AuthService())->check_opcode($code);
        $res=TurntableLog::where("id",$id)->update(["state"=>1]);
        return go($res);
    }

    public function dy_login()
    {
        $invite_uid="";
        if (isset($this->post["invite_uid"])){
            $invite_uid=$this->post["invite_uid"];
        }
        $infor=(new DyService())->code2Session($this->post["code"],$this->post["anonymous_code"]);
        $res=AuthService::dy_login($infor,$invite_uid);
        return go($res);
    }

    /**抖音绑定手机号码
     * @return array
     * @throws ApiException
     */
    public function dy_bind_phone()
    {
        $session_key = TokenService::getCurrentSessionKey();
        $detail = (new DyService())->getPhone($this->post["encryptedData"], $this->post["iv"], $session_key);
        if (isset($detail['phone'])) {

            User::where("id", TokenService::getCurrentUid())->update(["phone" => $detail["phone"]]);
            if (isset($this->post["type"])) {
                if ($this->post["type"] == "application") {
                    $model =new AppointmentModel();
                    $post = [
                        "uid" => TokenService::getCurrentUid(),
                        "time" => date("Y-m-d H:i:s"),
                        "name" => User::where("id", TokenService::getCurrentUid())->value("nickname"),
                        "phone" => $detail["phone"],
                        "state" => 0
                    ];

                    $res = $model->create($post);
                    return go($res);
                }
            } else {
                  $user = User::where("id", TokenService::getCurrentUid())->first();
            return go($user);
                throw new ApiException("绑定失败");
            }
            $user = User::where("id", TokenService::getCurrentUid())->first();
            return go($user);

        }else{
              $user = User::where("id", TokenService::getCurrentUid())->first();
            return go($user);
            throw new ApiException("绑定失败");
        }
    }



}
